package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/10 9:29
 **/
public class ImagefloodFill733 {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int oriColor = image[sr][sc];
        doFile(image, sr, sc,oriColor,color);
        return image;
    }

    public void doFile(int[][] image, int sr, int sc, int oriColor, int color) {
        if(sr<0 || sr>image.length-1 || sc<0 || sc>image[0].length-1) {
            return;
        }

        if (image[sr][sc] != oriColor) {
            return;
        }

        image[sr][sc] = color;
        int[][] visited = {{1,0},{-1,0},{0,1},{0,-1}};
        for(int i=0;i<visited.length; i++) {
            int r = sr+visited[i][0];
            int c = sc+visited[i][1];
            if(r >= 0 && r < image.length && c >= 0 && c < image[0].length && image[r][c] == color) {
                continue;
            }
            doFile(image, r,c,oriColor,color);

        }
    }
}
